package io.sage.modules.warn.controller;

import io.sage.common.utils.R;
import io.sage.common.utils.StringUtils;
import io.sage.modules.sys.entity.SysDeptEntity;
import io.sage.modules.sys.entity.SysUserEntity;
import io.sage.modules.sys.service.SysDeptService;
import io.sage.modules.sys.shiro.ShiroUtils;
import io.sage.modules.warn.entity.BRfidWarmConf;
import io.sage.modules.warn.service.BRfidWarmConfService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 报警类型配置(BRfidWarmConf)表控制层
 *
 * @author zxh
 * @since 2019-08-06 15:55:17
 */
@RestController
@RequestMapping("warn/brfidwarnconf")
public class BRfidWarmConfController {
    @Autowired
    private SysDeptService sysDeptService;
    /**
     * 服务对象
     */
    @Resource
    private BRfidWarmConfService bRfidWarmConfService;

    /**
     * 通过当前用户的总部门
     *查询单条数据
     *
     *
     * @return 单条数据
     */
    @GetMapping("/info")
    public R selectOne() {
        SysUserEntity user = ShiroUtils.getUserEntity();
        SysDeptEntity sysDeptEntity=sysDeptService.selectById(user.getDeptId());
        String no=sysDeptEntity.getDeptNo().substring(0,2);
        BRfidWarmConf bRfidWarmConf=new BRfidWarmConf();
        bRfidWarmConf.setDeptNo(no);
        List<BRfidWarmConf> list=this.bRfidWarmConfService.queryAll(bRfidWarmConf);
        return R.ok().put("bRfidWarnConf", list.size()>0?list.get(0):null);
    }
    /**
     * 通过当前用户的总部门
     *查询单条数据
     *
     *
     * @return 单条数据
     */
    @PostMapping("/saveOrUpdate")
    public R saveInfo(@RequestParam Map<String,String> param) {
        SysUserEntity user = ShiroUtils.getUserEntity();
        SysDeptEntity sysDeptEntity=sysDeptService.selectById(user.getDeptId());
        String no=sysDeptEntity.getDeptNo().substring(0,2);
        int i=0;
        if(StringUtils.isNotEmpty(param.get("id"))){
            BRfidWarmConf bRfidWarmConf=new BRfidWarmConf();
            bRfidWarmConf.setIsDel(0);
            bRfidWarmConf.setId(Integer.parseInt(param.get("id")));
            bRfidWarmConf.setDeptNo(no);
            bRfidWarmConf.setUpdateTime(new Date());
            bRfidWarmConf.setUpdateUser(user.getUserId());
            bRfidWarmConf.setType(param.get("type"));
            i=this.bRfidWarmConfService.update(bRfidWarmConf);
        }else{
            BRfidWarmConf bRfidWarmConf=new BRfidWarmConf();
            bRfidWarmConf.setDeptNo(no);
            bRfidWarmConf.setIsDel(0);
            bRfidWarmConf.setUpdateTime(new Date());
            bRfidWarmConf.setUpdateUser(user.getUserId());
            bRfidWarmConf.setCreateTime(new Date());
            bRfidWarmConf.setCreateUser(user.getUserId());
            bRfidWarmConf.setType(param.get("type"));
            i=this.bRfidWarmConfService.insert(bRfidWarmConf);
        }

        return i>0?R.ok():R.error();
    }

}